import { lazy } from 'react'
import { buildRoutes, type RouteConfig } from './utils'
import { createBrowserRouter } from 'react-router'

export type TRouteConfig = RouteConfig & { [x: string]: any }
const routeConfig: TRouteConfig[] = [
    {
        path: '/login',
        element: lazy(() => import('@/pages/login')),
    },
    {
        path: '/register',
        element: lazy(() => import('@/pages/register')),
    },
    {
        path: '/forgotPassword',
        element: lazy(() => import('@/pages/forgot-password')),
    },
    {
        path: '/tiptap',
        element: lazy(() => import('@/pages/tiptap')),
    },
    {
        path: "/",
        index: true,
        element: lazy(() => import('@/layouts/BasicLayout')),
        middlewares: [
            lazy(() => import('@/middlewares/AuthMiddleware'))
        ],
        children: [
            {
                index: true,
                path: 'home',
                element: lazy(() => import('@/pages/home')),
            }
        ],
    },
    {
        path: "/work",
        index: true,
        element: lazy(() => import('@/layouts/WorkLayout')),
        middlewares: [
            lazy(() => import('@/middlewares/AuthMiddleware'))
        ],
        children: [
            {
                index: true,
                path: 'dashboard',
                element: lazy(() => import('@/pages/work/dashboard')),
            },
            {
                index: true,
                path: 'center',
                element: lazy(() => import('@/pages/work/center')),
            },
            {
                index: true,
                path: 'ag',
                element: lazy(() => import('@/pages/work/test')),
            }
        ],
    },
    {
        path: "*",
        element: lazy(() => import('@/pages/404')),
    }
]

const fillRoutes = (routes: TRouteConfig[]) => {
    let _routes: TRouteConfig[] = []
    for (let index = 0; index < routes.length; index++) {
        const route = routes[index];
        if (route.children) {
            _routes = [..._routes, ...fillRoutes(route.children)]
        } else {
            _routes.push(route)
        }
    }
    return _routes
}
export const allRoutes = () => {
    return fillRoutes(routeConfig)
}

export const routes = buildRoutes(routeConfig)
export const router = createBrowserRouter(routes)